home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / files / example1.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  5KB  |  149 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Files                       Tulevagen 22       */
  8. /* File:    Example1.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-11                                       */
  11. /* Version: 1.1                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program collects ten integer values from the user, and  */
  21. /* saves them in a file called "HighScore.dat" on the RAM disk. */
  22.  
  23.  
  24.  
  25. /* Include the dos library definitions: */
  26. #include <dos/dos.h>
  27.  
  28. /* Now we include the necessary function prototype files:         */
  29. #include <clib/dos_protos.h>       /* General dos functions...    */
  30. #include <stdio.h>                 /* Std functions [printf()...] */
  31. #include <stdlib.h>                /* Std functions [exit()...]   */
  32.  
  33.  
  34.  
  35. /* Set name and version number: */
  36. UBYTE *version = "$VER: AmigaDOS/InputOutput/Example1 1.1";
  37.  
  38.  
  39.  
  40. /* Declared our own function(s): */
  41.  
  42. /* Our main function: */
  43. int main( int argc, char *argv[] );
  44.  
  45.  
  46.  
  47. /* Main function: */
  48.  
  49. int main( int argc, char *argv[] )
  50. {
  51.   /* A "BCPL" pointer to our file: */
  52.   BPTR my_file;
  53.  
  54.   /* The numbers: (10 integers will be saved) */
  55.   int my_highscore[ 10 ];
  56.  
  57.   /* Store here the number of bytes actually written: */
  58.   long bytes_written;
  59.  
  60.   /* A simple loop variable: */
  61.   int loop;  
  62.  
  63.  
  64.  
  65.   /* Let the user enter ten integer values: */
  66.   printf( "Please enter ten integer values:\n" );
  67.   for( loop=0; loop < 10; loop++ )
  68.   {
  69.     printf( "Value [%d]: ", loop );
  70.     scanf( "%d", &my_highscore[ loop ] );
  71.   }
  72.  
  73.  
  74.  
  75.   /* Try to open file "RAM:HighScore.dat" as a new file: (If */
  76.   /* the file does not exist, it will be created. If it, on  */
  77.   /* the the other hand, exist, it will be overwritten.)     */
  78.   my_file = Open( "RAM:HighScore.dat", MODE_NEWFILE );
  79.   
  80.   /* Have we opened the file successfully? */
  81.   if( !my_file )
  82.   {
  83.     /* Inform the user: */
  84.     printf( "Error! Could not open the file!\n" );
  85.  
  86.     /* Exit with an error code: */
  87.     exit( 20 );
  88.   }
  89.  
  90.   /* The file has now been opened: */
  91.   printf( "File open!\n" );
  92.  
  93.  
  94.  
  95.   /* We have now opened a file and the file cursor is  */
  96.   /* pointing to the first byte (character) in our new */
  97.   /* file. We can now start to write:                  */
  98.   bytes_written = Write( my_file, my_highscore, sizeof( my_highscore ) );
  99.  
  100.   /* Did we write all data? */ 
  101.   if( bytes_written != sizeof( my_highscore ) )
  102.   {
  103.     /* No! The numbers actually written was less */
  104.     /* than we wanted to write!                  */
  105.     printf( "Error! Could not save all values!\n" );
  106.   }
  107.   else
  108.   {
  109.     /* Yes, all numbers have been written to the file! */
  110.     printf( "All values were saved successfully!\n" );
  111.   }
  112.  
  113.   /* Since we store 10 integer values the file should be 40 bytes  */
  114.   /* long. 1 integer (32 bits) = 4 bytes, 10 integers (320 bits) = */
  115.   /* 40 bytes.                                                     */
  116.  
  117.  
  118.  
  119.   /* Close the file. With V36 or higher the Close() function  */
  120.   /* will return a boolean value, TRUE if the file was        */
  121.   /* successfully closed, FALS if the file could not be       */
  122.   /* closed. If the file could not be closed there is sadly   */
  123.   /* very little we can do about it. We should never try to   */
  124.   /* close a file after it has been closed, successfully or   */
  125.   /* not! Even if the actual file could not be closed most of */
  126.   /* the memory used by the filehandler will still have been  */
  127.   /* deallocated.                                             */
  128.   /*                                                          */
  129.   /* In most cases you can simply ignore what the Close()     */
  130.   /* function returns since you can not do much about it.     */
  131.   /* However, if you have saved important data in the file    */
  132.   /* you might want to open a new file and save it all again  */
  133.   /* just to be on the safe side. Before you may do this you  */
  134.   /* should of course ask for the user's permission.          */
  135.   if( Close( my_file ) )
  136.     printf( "File closed!\n" );
  137.   else
  138.     printf( "Error! File could not be closed!\n" );
  139.  
  140.   /* Remember that even if the file could not be */
  141.   /* closed we must NOT try to close it again!   */
  142.   
  143.   
  144.   
  145.   /* The End! */
  146.   exit( 0 );
  147. }
  148.  
  149.